package com.moyu.frame;

import com.moyu.control.SaveEnterControl;
import com.moyu.activity.PanelGame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FrameSavePoint extends JFrame {
    private JLabel labelpoint;
    private JButton buttonSub;
    private JTextField textname;
    PanelGame panelGame;
    public FrameSavePoint(PanelGame panelGame){
        this.panelGame = panelGame;
        int width = 256;
        int height = 128;
        this.setTitle("保存分数");
        this.setLocationRelativeTo(null);
        this.setSize(width,height);
        this.setResizable(false);


        //屏幕大小
        int screenwidth = Toolkit.getDefaultToolkit().getScreenSize().width;
        int screenheight = Toolkit.getDefaultToolkit().getScreenSize().height;
        //窗体居中
        this.setBounds((screenwidth - width) / 2,(screenheight - height) / 2, width, height);
        //创建组件
        createCom();
        //创建按键监听
        textname.addKeyListener(new SaveEnterControl(this,panelGame,textname));
        //创建按钮监听
        createAction(this);
        //窗体可见
        this.setVisible(true);

    }

    public void createCom(){
        //上
        Font font = new Font("黑体",Font.BOLD,15);
        this.labelpoint = new JLabel("你的分数为"+panelGame.getTotalpoint());
        labelpoint.setFont(font);
        JPanel north = new JPanel(new FlowLayout(FlowLayout.LEFT));
        north.add(labelpoint);
        this.add(north,BorderLayout.NORTH);
        //中
        this.textname = new JTextField(14);
        JPanel center = new JPanel(new FlowLayout(FlowLayout.LEFT));
        JLabel tname = new JLabel("你的名字:");
        tname.setFont(font);
        center.add(tname);
        center.add(textname);
        this.add(center,BorderLayout.CENTER);
        //下
        this.buttonSub = new JButton("确定");
        buttonSub.setFont(font);
        JPanel south = new JPanel(new FlowLayout(FlowLayout.CENTER));
        south.add(buttonSub);
        this.add(south,BorderLayout.SOUTH);
    }

    public void createAction(FrameSavePoint frameSavePoint){
        FrameSavePoint itself = frameSavePoint;
        this.buttonSub.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!textname.getText().equals("")){
                    //写入文件
                    panelGame.addpoint(textname.getText());
                }
                //按钮按完后隐藏窗体
                itself.setVisible(false);
            }
        });
    }
}
